package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.sun.javaws.security.AppPolicy;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService{

    // 查询敏感词
    @Override
    public ResponseResult findSensitive(SensitiveDTO sensitiveDTO) {
        // 设置参数
        // 判断前端传的数据是否为空
        if (sensitiveDTO == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"数据不能为空");
        }
        // 分页设置
        sensitiveDTO.checkParam();
        // 分页
        Page<AdSensitive> page = new Page<>(sensitiveDTO.getPage(), sensitiveDTO.getSize());
        // 查询
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.<AdSensitive>lambdaQuery();
        if (StringUtils.isNotBlank(sensitiveDTO.getName())){
            wrapper.like(AdSensitive::getSensitives,sensitiveDTO.getName());
        }
        IPage<AdSensitive> iPage = this.page(page, wrapper);
        return new PageResponseResult(sensitiveDTO.getPage(),sensitiveDTO.getSize(),iPage.getTotal(), iPage.getRecords());
    }

    // 添加敏感词
    @Override
    public ResponseResult addSensitive(AdSensitive adSensitive) {
        // 校验参数
        // 判断敏感词是否为空
        if (adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词不能为空");
        }
        // 敏感词长度不能超过10位
        if (adSensitive.getSensitives().length() > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词长度不能超过10位");
        }
        // 判断敏感词是否已经存在
        AdSensitive sensitive = this.getOne(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (sensitive != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已经存在");
        }
        adSensitive.setCreatedTime(new Date());
        // 添加敏感词
        this.save(adSensitive);
        return ResponseResult.okResult();
    }

    // 修改敏感词
    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        // 判断前端传的参数是否为空
        if (adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 判断敏感词是否为空
        if (StringUtils.isBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词不能为空");
        }
        // 判断敏感词长度
        if (adSensitive.getSensitives().length() > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词长度不能超过10位");
        }
        // 判断敏感词是否存在
        AdSensitive sensitive = this.getOne(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (sensitive != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已经存在");
        }
        // 修改敏感词
        this.updateById(adSensitive);
        // 响应
        return ResponseResult.okResult();
    }

    // 删除敏感词
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        // 判断ID是否为空
        if (id ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"ID不能为空");
        }
        // 删除敏感词
        this.removeById(id);
        return ResponseResult.okResult();
    }
}
